home *** CD-ROM | disk | FTP | other *** search
/ Mac Expert 1995 Winter / Mac Expert - Winter 95.iso / Les fichiers / Utilitaires divers / Divers / C64-Emulator 0.4 ƒ / SOURCE / CPU68K.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-02  |  4.0 KB  |  177 lines  |  [TEXT/KAHL]

  1. /*
  2.     Commodore 64 Emulator v0.4      Earle F. Philhower III 
  3.     Copyright (C) 1993-4            (st916w9r@dunx1.ocs.drexel.edu)
  4.  
  5.     (Somewhat)
  6.     High Speed 68K CPU simulation by George T. Talbot
  7.                                      (ugtalbot@mcs.drexel.edu)
  8.  
  9.     This program is free software; you can redistribute it and/or modify
  10.     it under the terms of the GNU General Public License as published by
  11.     the Free Software Foundation; either version 2 of the License, or
  12.     (at your option) any later version.
  13.  
  14.     This program is distributed in the hope that it will be useful,
  15.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.     GNU General Public License for more details.
  18.  
  19.     You should have received a copy of the GNU General Public License
  20.     along with this program; if not, write to the Free Software
  21.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. */
  23.  
  24. #ifndef __MWERKS__
  25.  
  26. #include "Processor.h"
  27. #include "Registers68K.h"
  28. #include "Flags68K.h"
  29. #include "Instructions68K.h"
  30. #include "Stack68K.h"
  31. #include "Modes68K.h"
  32. #include "Memory68K.h"
  33. #include "Keyboard.h"
  34.  
  35. #define    ONE_SCAN_LINE        35    /* CPU Cycles */
  36. #define    ONE_FRAME            350    /* Screen lines    */
  37.  
  38. extern word (* instructf[256]) ();
  39. extern byte cycletimef[256];
  40. extern byte    *RAM, **memory;
  41.  
  42. #define    SETUP_CPU                    \
  43.     tsp            = sp;                \
  44.     tpc            = pc;                \
  45.     asm    {                            \
  46.         MOVEM.L    D3-D7/A2-A3,-(A7)    \
  47.         MOVE.W    tpc,rPC                \
  48.         MOVE.W    tsp,rSP                \
  49.         MOVE.B    a,rA                \
  50.         MOVE.B    x,rX                \
  51.         MOVE.B    y,rY                \
  52.         MOVE.B    flags,rFLAGS        \
  53.         MOVEA.L    RAM,rRAM            \
  54.         MOVE.L    rRAM,rSTACK            \
  55.         ADDA.L    #256,rSTACK            \
  56.         MOVEA.L    memory,rMEMORY        \
  57.         }
  58.  
  59. #define    RESTORE_CPU                    \
  60.     asm    {                            \
  61.         MOVE.W    rPC,tpc                \
  62.         MOVE.W    rSP,tsp                \
  63.         MOVE.B    rA,a                \
  64.         MOVE.B    rX,x                \
  65.         MOVE.B    rY,y                \
  66.         MOVE.B    rFLAGS,flags        \
  67.         MOVEM.L    (A7)+,D3-D7/A2-A3    \
  68.         }                            \
  69.     sp    = tsp;                        \
  70.     pc    = tpc;
  71.  
  72. /*    Had to enclose SetUpMemoryMap() by the CPU initialization so that the CPU isn't
  73.  *    disturbed when SetUpMemoryMap() is called, and so that the CPU registers can be
  74.  *    used in SetUpMemoryMap(), if necessary.
  75.  */
  76. void    redoMemoryMap(void)
  77.     {
  78.     word        tsp, tpc;
  79.  
  80.     RESTORE_CPU
  81.     SetUpMemoryMap();
  82.     SETUP_CPU
  83.     }
  84.  
  85. #pragma nooptimize(redoMemoryMap)
  86.  
  87. /*    This function is a translation of the processor loop in Processor.c.    */
  88. void    CPU()
  89.     {
  90.     word        tsp, tpc;
  91.     byte        cur_cycles;
  92.     byte        oldMemoryMap;
  93.     word        cur_scanLines;
  94.  
  95.     oldMemoryMap    = RAM[1];
  96.     cur_scanLines    = 0;
  97.     cur_cycles        = 0;
  98.  
  99.     /*    Put the 6510 registers in 68K registers.  (See Registers68K.h)    */
  100.     SETUP_CPU
  101.  
  102.     /*    Outside loop is one screen frame    */
  103.     do    {
  104.         /*    Inside loop is one screen line    */
  105.         do    {
  106.             asm    {
  107.                 /*    Fetch the current instruction    */
  108.                 CLR.L    D1
  109.                 MOVE.W    rPC,D1
  110.                 LSL.L    #2,D1
  111.                 MOVEA.L    (rMEMORY,D1.L),A2
  112.                 MOVE.B    (A2),D1
  113.                 ANDI.W    #0x00FF,D1
  114.  
  115.                 /*    Advance PC past the instruction byte (additional PC
  116.                  *    advances will be done in the instructions, themselves).
  117.                  */
  118.                 PC(#1)
  119.  
  120.                 /*    Count up the CPU cycles executed so far    */
  121.                 MOVE.B    cur_cycles,D0
  122.                 LEA        cycletimef,A2
  123.                 ADD.B    (A2,D1.W),D0
  124.                 MOVE.B    D0,cur_cycles
  125.  
  126.                 /*    Execute the instruction    */
  127.                 LSL.W    #2,D1
  128.                 LEA        instructf,A2
  129.                 MOVE.L    (A2,D1.W),A2
  130.                 JSR        (A2)
  131.  
  132.                 /*    Set up the keyboard scan codes    */
  133.                 CLR.W    D0
  134.                 MOVE.L    #0x0000DC00,D1
  135.                 MOVE.B    (rRAM,D1.L),D0
  136.                 LEA        scanCode,A2
  137.                 MOVE.B    (A2,D0.W),D0
  138.                 ADDQ.W    #1,D1
  139.                 MOVE.B    D0,(rRAM,D1.L)
  140.  
  141.                 /*    Check for memory map changes, and change the memory map, if so    */
  142.                 MOVE.B    1(rRAM),D0
  143.                 CMP.B    oldMemoryMap,D0
  144.                 BEQ        @1
  145.                 MOVE.B    D0,oldMemoryMap
  146.                 BSR        redoMemoryMap
  147. @1
  148.                 }
  149.             }
  150.         while (cur_cycles < ONE_SCAN_LINE);
  151.  
  152.         cur_cycles    -= ONE_SCAN_LINE;
  153.  
  154.         /* Update the current VIC scan register */
  155.         asm    {
  156.             MOVE.L    #0x0000D012,D1
  157.             ADDQ.B    #1,(rRAM,D1.L)
  158.             BNE        @2
  159.             SUBQ.L    #1,D1
  160.             EORI.B    #0x80,(rRAM,D1.L)
  161. @2            MOVE.L    #0x0000D018,D1
  162.             ORI.B    #1,(rRAM,D1.L)
  163.             }
  164.  
  165.         ++cur_scanLines;
  166.         }
  167.     while (cur_scanLines <= ONE_FRAME);
  168.  
  169.     /*    Put the 6510 registers back into the memory variables    */
  170.     RESTORE_CPU
  171.     }
  172.  
  173. #pragma nooptimize(CPU)
  174.  
  175.  
  176. #endif
  177.